import { _decorator, animation, Animation, Collider2D, Component, Enum, Node } from 'cc';
import { PlantState, PlantType } from './Global';
const { ccclass, property } = _decorator;


@ccclass('Plant')
export class Plant extends Component {
    plantState: PlantState = PlantState.Disable;

    @property({ type: Enum(PlantType) })
    public plantType: PlantType;

    @property({ type: Number })
    HP: number = 0;
    protected start(): void {
        this.transitionToDisable();
    }
    update(deltaTime: number) {
        // 根据植物的状态来执行相应的更新操作
        switch (this.plantState) {
            // 如果植物状态为禁用
            case PlantState.Disable:
                // 禁用更新操作
                this.disableUpdate();
                break;
            // 如果植物状态为启用
            case PlantState.enable:
                // 启用更新操作
                this.enableUpdate();
                break;
        }
    }
    /**
     * 未激活状态更新逻辑
     */
    disableUpdate() {
    }

    /**
     * 激活状态更新逻辑
     */
    enableUpdate() {

    }

    transitionToDisable() {
        // 将植物状态设置为禁用状态
        this.plantState = PlantState.Disable;
        // 获取动画控制器组件，并将其禁用
        if (this.getComponent(animation.AnimationController))
            this.getComponent(animation.AnimationController).enabled = false;
        if (this.getComponent(Animation))
            this.getComponent(Animation).enabled = false;
        this.getComponent(Collider2D).enabled = false;
    }
    transitionToEnable() {
        // 设置植物状态为启用
        this.plantState = PlantState.enable;
        // 获取动画控制器组件，并启用它
        if (this.getComponent(animation.AnimationController)) {
            this.getComponent(animation.AnimationController).enabled = true;
        }
        if (this.getComponent(Animation)) {
            this.getComponent(Animation).enabled = true;
        }
        this.getComponent(Collider2D).enabled = true;
    }

    takeDamage(damage: number) {
        // 从当前生命值中减去受到的伤害
        this.HP -= damage;
        // 如果生命值小于等于0
        if (this.HP <= 0) {
            // 调用死亡方法
            this.die();
        }
    }
    die() {
        this.node.destroy();
    }
}


